// pages/games/rocket/entry/entry.js
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:[],
    timu_num:0,//题目数量
    index:0,//当前题目下标
    status: 'error',
    type:1, //1为选选选 2为火箭发射
    jieguo:[], //答题结果
    uid:0,
    game_type:0,
    is_start:0 ,//是否点击了开始
    top:"70vh" ,//火箭移动控制变量
    cuowu_num:0 ,//错误题数
    is_jixu:0
  },
  //选择答案
  dati:function(e)
  {
    
    var that=this
    //判断是否是最后一题
     console.log(e)
     var timu_id=e.currentTarget.dataset.id
     var timu_xuanxiang=e.currentTarget.dataset.xuanxiang //1选项一 2选项二
    var jieguo=that.data.jieguo
    var item={id:timu_id,xuanxiang:timu_xuanxiang}

    //判断当前题目是否回答正确
    var cuowu_num=that.data.cuowu_num
    for (let o = 0; o < that.data.data.length; o++) {
      if(timu_id==that.data.data[o]['id'] && timu_xuanxiang== that.data.data[o]['daan'])
      {
           console.log("回答正确")
           that.setData({
             top:"0vh"
           })
           setTimeout(function(){
            that.setData({
              top:"40vh"
            })
          },1500)
      }
      if(timu_id==that.data.data[o]['id'] && timu_xuanxiang!= that.data.data[o]['daan']){
        console.log("回答错误")
        cuowu_num++;
        that.setData({
          cuowu_num:cuowu_num,
          top:"70vh"
        })
        setTimeout(function(){
          that.setData({
            top:"40vh"
          })
        },1500)
      }

      
    }
   
    //判断答案是否重复
    var a=0;
    for(var i=0;i<jieguo.length;i++)
    {
      var id=jieguo[0]['id']

      if(id==timu_id)
      {
         jieguo[i]=item
      }else{
        a++;
      }
    }
   if(a==jieguo.length)
   {
     jieguo.push(item)
   }

   console.log("当前答题错误"+cuowu_num+"道")

   //如果错误三道
   if(cuowu_num>=3)
   {
      wx.showToast({
        title: '错误三次，挑战失败',
        icon:"none",
        success(res)
        {
         that.addJieguo(that.data.jieguo)
        }
      })
 
      return false

   }

   wx.setStorage({
     key:"jieguo",
     data:that.data.jieguo
   })

   wx.setStorage({
    key:"timu_index",
    data:parseInt(that.data.index+1)
  })

   that.setData({
    index:parseInt(that.data.index+1)
   })


        console.log(that.data.jieguo)
    //如果到达最后一题结束游戏
    if(that.data.jieguo.length==that.data.data.length)
    {
      console.log("游戏结束")
      that.setData({
        is_start:0
      })
      that.addJieguo(that.data.jieguo)
    }else{
      console.log("游戏继续")
    }
  },
  //结束游戏提交数据
 addJieguo:function(jieguo)
 {
  var that=this
  wx.request({
    url: app.data.service_url+'game/addJieguo', //改成你服务端的方法
    data: {
      uid:that.data.uid,
      jieguo:JSON.stringify(that.data.jieguo),
      game_type:that.data.game_type,
      type:2 //选选
    },
    method:"POST",
    header:{
      "Content-Type": "application/x-www-form-urlencoded"
       },  
    success: function(res) {
      console.log(res)
      var code=res.data['code']
      if(code==1)
      {
      var jieguo_id=res.data['jieguo_id']
      wx.showToast({
        title: '挑战结束',
        icon:"none",
        success(res)
        {
          setTimeout(function(){
              wx.navigateTo({
            url: '/pages/games/hjfs/res?jieguo_id='+jieguo_id,
             })
            },800)
        
        }
      })

      }else{
        wx.showToast({
          title: '网络错误',
          icon:"none",
          success()
          {
            wx.switchTab({
              url: '/pages/tabs/home/home',
            })
          }
        })
      }


      wx.removeStorage({
        key: 'timu_index',
      })
      wx.removeStorage({
        key: 'jieguo',
      })

    }
  })

 },


    /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     var that=this
     wx.getStorage({
       key:"uid",
       success(res)
       {
         that.setData({
           uid:res.data
         })
       }
     })

     //提示用户是否继续上一句游戏
    wx.getStorage({
      key:"timu_index",
      success(res)
      {
        var index=res.data
        that.setData({
          index:index
        })
        wx.getStorage({
          key:"jieguo",
          success(res1)
          {
            var jieguo=res1.data

            wx.showModal({
              title: '提示',
              content: '是否返回上一局',
              success (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  that.setData({
                    index:index,
                    jieguo:jieguo,
                    is_jixu:1
                  })

                } else if (res.cancel) {
                  console.log('用户点击取消')
                  that.setData({
                    index:0,
                    jieguo:[],
                    is_jixu:0
                  })

                  wx.removeStorage({
                    key: 'timu_index',
                  })
                  wx.removeStorage({
                    key: 'jieguo',
                  })
                }
              }
            })
            
       
          }
        })
      }
    })

  },
  
  //随机获取获取题目 10条
  getTimu:function(game_type)
  {
    var that=this
    wx.request({
      url: app.data.service_url+'game/getTimu', //改成你服务端的方法
      data: {
        type:2, //火箭发射
        game_type:game_type
      },
      method:"POST",
      header:{
        "Content-Type": "application/x-www-form-urlencoded"
         },  
      success: function(res) {
        console.log(res)
        if(res.data.length==0)
        {
          wx.showToast({
            title: '没有题目',
            icon:"error",
            success()
            {
              setTimeout(function(){
                   wx.switchTab({
                 url: '/pages/tabs/home/home',
                  })
              },1000)
           
            }
          })
          return false
        }

    
           that.setData({
          data:res.data,
          timu_num:res.data.length
        })
    
    

   

      }
    })




  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that=this
      var game_type=options.game_type
      that.getTimu(game_type)
      that.setData({
        game_type:game_type
      })

    that.handle()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  handle(){
   console.log("游戏开始")
    var that=this
     
    that.setData({
      is_start:1,
      top:"40vh"
    })
  },

  next(){

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})